<template>
	<view class="">
		<view class="content wrap">
			<u-button type="warning" @click="binding()">条码绑定</u-button>
			<span class="u-demo-block__title weikaishi2 margin-b20" v-html="'商品编码：'+form.goodsNumber"></span>
			<u-form :model="form" ref="uForm">
				<u-form-item label="商品条码" label-width="200" left-icon="coupon">
					<u-input focus v-model="form.goodsNumber69" placeholder="请输入商品条码" @input="input1()"/>
					<u-icon @click="scan()" slot="right" name="scan" size="60"></u-icon>
				</u-form-item>
				
				<u-form-item label="对比条码" label-width="200" left-icon="coupon-fill">
					<u-input v-model="form.goodsNumber269" placeholder="请输入对比条码" @input="input2()"/>
					<u-icon @click="scan2()" slot="right" name="scan" size="60"></u-icon>
				</u-form-item>
			</u-form>
			<u-button type="default" size="default" @click="qingkong()">清空</u-button>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				flex:'',
				form:{
					goodsNumber:"",
				    goodsNumber69:"",
				    goodsNumber269:"",
				},
			};
		},
		onLoad(open) {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif
		},
		methods:{
			binding(){
				this.$Router.push({
					name: 'goods_install'
				})
			},
			scan(){
				let that = this
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.form.goodsNumber69 = res.result
						if(res.result != null && res.result != ''){
							uni.showToast({
								title: "识别成功"
							})
						}
						that.input1(res.result)
					}
				});
			},
			scan2(){
				let that = this
				uni.scanCode({
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.form.goodsNumber269 = res.result
						if(res.result != null && res.result != ''){
							uni.showToast({
								title: "识别成功"
							})
						}
						that.input2(res.result)
					}
				});
			},
			input1(e){
				if(this.form.goodsNumber69 != null && this.form.goodsNumber69 != ''){
					this.$http.post('/rest/tool/getToolGoodsInstall',{
					},{
						params: {v1:this.form.goodsNumber69},
						header: {
							'Authorization' :uni.getStorageSync('token'),
						}
					}).then(res => {
						if(res.data.data != null && res.data.data != '') {
							this.form.goodsNumber = res.data.data.goodsNumber
							if(this.form.goodsNumber69 != null && this.form.goodsNumber69 != '' && this.form.goodsNumber269 != null && this.form.goodsNumber269 != ''){
								if(this.form.goodsNumber69 == this.form.goodsNumber269){
									this.audio_success()
								} else {
									this.audio_error()
								}
							} 
						} else {
							this.form.goodsNumber = ''
						}
					})
				}
			},
			input2(e){
				if(this.form.goodsNumber69 != null && this.form.goodsNumber69 != '' && this.form.goodsNumber269 != null && this.form.goodsNumber269 != ''){
					if(this.form.goodsNumber69 == this.form.goodsNumber269){
						this.audio_success()
					} else {
						this.audio_error()
					}
				} 
				
				this.$http.post('/rest/tool/qingqiu',{
				},{
					params: {v1:this.form.goodsNumber69},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.form.goodsNumber269 = ""
				})
			},
			qingkong(){
				this.$http.post('/rest/tool/qingqiu',{
				},{
					params: {v1:this.form.goodsNumber69},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					this.form.goodsNumber = ""
					this.form.goodsNumber69 = ""
					this.form.goodsNumber269 = ""
				})
			},
			audio_success(){
				//验证商品编号一致  播报语音
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = '../../static/audio/success.mp3';
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
				innerAudioContext.onPause(function() {
					innerAudioContext.destroy();//音频播放停止后，就销毁实例
				});
			},
			audio_error(){
				//验证商品编号不一致  播报语音
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = '../../static/audio/error.mp3';
				innerAudioContext.onPlay(() => {
				  console.log('开始播放');
				});
				innerAudioContext.onError((res) => {
				  console.log(res.errMsg);
				  console.log(res.errCode);
				});
				innerAudioContext.onPause(function() {
					innerAudioContext.destroy();//音频播放停止后，就销毁实例
				});
			},
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	.wrap {
		padding: 30rpx;
		margin: 0 auto;
	}
	.weikaishi2{
		font-size: 36rpx;
	}
	.weikaishi3{
		font-size: 32rpx;
	}
	.huanhang{
		word-break:break-all;
	}
</style>
